package cn.tedu.luban.account.main.service;

import cn.tedu.luban.account.main.converter.AccountConverter;
import cn.tedu.luban.account.main.repository.AccountRepository;
import cn.tedu.luban.account.protocol.bo.AccountBO;
import cn.tedu.luban.account.protocol.dos.AccountDO;
import cn.tedu.luban.account.protocol.dto.AccountDTO;
import cn.tedu.luban.account.protocol.param.AccountCreateParam;
import cn.tedu.luban.account.protocol.param.AccountPayParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AccountService {

    @Autowired
    private AccountConverter accountConverter;

    @Autowired
    private AccountRepository accountRepository;

    public Long createAccount(AccountCreateParam accountCreateParam) {
        Long result = 0L;
        // 1. 使用userId查询账号是否存在
        Long userId = accountCreateParam.getUserId();
        AccountBO accountBO = accountRepository.getAccountByUserId(userId);

        if(accountBO.getId() == null){
            //创建账号
            result = accountRepository.save(accountCreateParam);
        }

        return result;

    }

    public AccountDTO getAccount(Long userId){
        AccountBO accountBO = accountRepository.getAccountByUserId(userId);
        return accountConverter.bo2dto(accountBO);
    }

    public Long mockPay(AccountPayParam param) {
        return accountRepository.updateTotalAmount(param);
    }
}
